<template>
    <div>
        <el-form ref="CustomerForm" :inline="true" :model="CustomerForm" class="tpl-form-inline pb0">
            <el-form-item label="时间">
                <el-date-picker v-model="CustomerForm.birthday" type="daterange" range-separator="~" format="yyyy 年 MM 月 dd 日" :picker-options="pickerOptions"
                    start-placeholder="开始日期" end-placeholder="结束日期"> </el-date-picker>
            </el-form-item>
            <el-form-item label="保司">
                <el-select v-model="CustomerForm.safeName" placeholder="全部">
                    <el-option label="阳光保险" value='阳光保险'></el-option>
                    <el-option label="中国平安保险" value='中国平安保险'></el-option>
                    <el-option label="太平洋保险" value='太平洋保险'></el-option>
                    <el-option label="中国人保" value='中国人保'></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="主机厂">
                <el-select v-model="CustomerForm.hostName" placeholder="全部">
                    <el-option label="吉利汽车" value="吉利汽车"></el-option>
                    <el-option label="长城汽车" value="长城汽车"></el-option>
                    <el-option label="东风汽车" value="东风汽车"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="保险状态">
                <el-select v-model="CustomerForm.safeStatus" placeholder="全部">
                    <el-option label="待起保" value="待起保"></el-option>
                    <el-option label="保障中" value="保障中"></el-option>
                    <el-option label="已脱保" value="已脱保"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="VIN" prop="secContName">
                <el-input v-model.trim="CustomerForm.vCode" placeholder="请输入"></el-input>
            </el-form-item>
        </el-form>
        <div class="mt10 mb20">
            <el-button type="primary" icon="el-icon-search" @click="queryList" :disabled="loading">查询</el-button>
            <el-button @click="reset" icon="el-icon-refresh" :disabled="loading">重置</el-button>
            <el-button type="primary" icon="el-icon-download" @click="exportFile" >导出</el-button>
        </div>

        <div class="text item">
            <el-table class="sd" stripe border :data="tableData" v-loading="loading">
                <el-table-column align="center" prop="index" label="序号" width="100"></el-table-column>
                <el-table-column align="center" prop="safeNo" label="保单号"></el-table-column>
                <el-table-column align="center" prop="safeName" label="保司"></el-table-column>
                <el-table-column align="center" prop="hostName" label="主机厂"></el-table-column>
                <el-table-column align="center" prop="safeStatus" label="保险状态"></el-table-column>
                <el-table-column align="center" prop="isOpen" label="续保情况"></el-table-column>
                <el-table-column align="center" prop="vCode" label="车辆VIN码"></el-table-column>
                <el-table-column align="center" prop="bonus" label="累计奖励金"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="handleEdit(scope.$index, scope.row)">保单详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination class="tpl-mg-t mt20 fr"
                            small
                            @size-change="pageSizeChange"
                            @current-change="pageCurrentChange"
                            :current-page="CustomerForm.pageNum"
                            :page-sizes="[10, 20, 30, 40, 50, 100]"
                            :page-size="CustomerForm.pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total">
            </el-pagination>
        </div>

    </div>
</template>

<script>
    export default {
        name: 'policyInfo',
        data () {
            return {
                // 查询表单
                CustomerForm: {
                    birthday: '',
                    safeName: '',
                    hostName: '',
                    safeStatus: '',
                    vCode: '',
                },
                // 数据总条数
                total: 0,
                // 表格数据
                tableData: [],
                newData: [],
                pickerOptions: {
                    shortcuts: [{
                        text: '今天',
                        onClick (picker) {
                            const start = new Date()
                            picker.$emit('pick', [start, start])
                        }
                    }]
                },
                params: {},
                loading: false
            }
        },
        created () {
            this.getList();
        },
        mounted () {
        },
        computed: {
        },
        methods: {
            handleEdit (i,r) {
                this.$routerTo('editPolicy', { policyData: r, callback: this.getList });
            },
            // 点击查询置为第一页
            queryList () {
                // this.CustomerForm.pageNum = 1
                this.params = { ...this.CustomerForm }
                delete this.params.birthday
                for (let key in this.params) {
                    if (!this.params[key] || this.params[key] === '' || this.params[key].length === 0) {
                        delete this.params[key]
                    }
                }
                if (this.CustomerForm.birthday && this.CustomerForm.birthday.length === 2) {
                    this.params.birthdayStart = new Date(new Date(new Date(this.CustomerForm.birthday[0]).setHours(0)).setMinutes(0)).setSeconds(0)
                    this.params.birthdayEnd = new Date(new Date(new Date(this.CustomerForm.birthday[1]).setHours(0)).setMinutes(0)).setSeconds(0) + 24 * 3600 * 1000
                }
                this.tableData = this.$filters(this.params, this.newData);
            },
            // 获取列表
            getList () {
                let cData = JSON.parse(localStorage.getItem('policyData'));
                if(!cData){
                    cData = [
                        {"index": "1","safeNo": "263994483758472","safeName": "阳光保险","hostName": "吉利汽车","safeStatus": "待起保","isOpen": "无","vCode": "LVSHJXXXXXX384344","bonus": "0元"},
                        {"index": "2","safeNo": "263994483758472","safeName": "中国平安保险","hostName": "长城汽车","safeStatus": "保障中","isOpen": "一年","vCode": "LVSHJXXXXXX384344","bonus": "1000元"},
                        {"index": "3","safeNo": "263994483758472","safeName": "太平洋保险","hostName": "东风汽车","safeStatus": "已脱保","isOpen": "两年","vCode": "LVSHJXXXXXX384344","bonus": "1000元"},
                        {"index": "4","safeNo": "263994483758472","safeName": "中国人保","hostName": "吉利汽车","safeStatus": "待起保","isOpen": "三年","vCode": "LVSHJXXXXXX384344","bonus": "0元"},
                        {"index": "5","safeNo": "263994483758472","safeName": "阳光保险","hostName": "吉利汽车","safeStatus": "待起保","isOpen": "无","vCode": "LVSHJXXXXXX384344","bonus": "0元"},
                        {"index": "6","safeNo": "263994483758472","safeName": "中国平安保险","hostName": "长城汽车","safeStatus": "保障中","isOpen": "一年","vCode": "LVSHJXXXXXX384344","bonus": "1000元"},
                        {"index": "7","safeNo": "263994483758472","safeName": "太平洋保险","hostName": "东风汽车","safeStatus": "已脱保","isOpen": "两年","vCode": "LVSHJXXXXXX384344","bonus": "1000元"},
                        {"index": "8","safeNo": "263994483758472","safeName": "中国人保","hostName": "吉利汽车","safeStatus": "待起保","isOpen": "三年","vCode": "LVSHJXXXXXX384344","bonus": "0元"},
                        {"index": "9","safeNo": "263994483758472","safeName": "阳光保险","hostName": "吉利汽车","safeStatus": "待起保","isOpen": "无","vCode": "LVSHJXXXXXX384344","bonus": "0元"},
                        {"index": "10","safeNo": "263994483758472","safeName": "中国平安保险","hostName": "长城汽车","safeStatus": "保障中","isOpen": "一年","vCode": "LVSHJXXXXXX384344","bonus": "1000元"},
                        {"index": "11","safeNo": "263994483758472","safeName": "太平洋保险","hostName": "东风汽车","safeStatus": "已脱保","isOpen": "两年","vCode": "LVSHJXXXXXX384344","bonus": "1000元"}
                    ]
                }
                localStorage.setItem('policyData', JSON.stringify(cData));
                this.tableData = cData;
                this.newData = cData;
                // this.CustomerForm.pageNum = 1
                this.total = this.tableData.length;
            },
            // 重置表单
            reset () {
                this.CustomerForm = {
                    birthday: '',
                    safeName: '',
                    hostName: '',
                    safeStatus: '',
                    vCode: '',
                }
                this.$nextTick(() => {
                    this.getList()
                })
            },
            exportFile () {
                if(this.CustomerForm.birthday[0] || this.CustomerForm.vCode != "" || this.CustomerForm.bsType !== "" || this.CustomerForm.bxType !== "" || this.CustomerForm.zjType !== ""){
                    this.$message({ type: 'success', message: `导出成功!` })
                }else{
                    this.$message.warning('请先选择筛选条件！')
                }
            },
            // 每页显示条数
            pageSizeChange (val) {
                let pages = Math.ceil(this.total / val)
                this.CustomerForm.pageSize = val
                this.params.pageSize = val
                if (this.CustomerForm.pageNum > pages) {
                    this.CustomerForm.pageNum = 1
                    this.params.pageNum = 1
                }
                this.queryList()
            },
            // 当前页码
            pageCurrentChange (val) {
                this.CustomerForm.pageNum = val
                this.params.pageNum = val
                this.queryList()
            }
        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped rel="stylesheet/scss">

</style>
